package top.deepdesigner.controller.user;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.dto.AccountTraceDTO;
import top.deepdesigner.pojo.DtsAccountTrace;
import top.deepdesigner.service.DtsAccountTraceService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.AccountTraceVo;

import java.util.HashMap;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/21 星期三 21:59:03 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/brokerage")
public class BrokerageController {

  @Autowired
  private DtsAccountTraceService dtsAccountTraceService;

  //获取佣金列表
  @RequiresPermissionsDesc(menu = {"用户管理", "佣金管理"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:account:list"})
  @GetMapping("/list")
  public Object list(@RequestParam HashMap<String, Object> params,
                     @RequestParam(defaultValue = "") String username,
                     @RequestParam(defaultValue = "") String mobile,
                     @RequestParam(defaultValue = "") Integer[] statusArray) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");

    AccountTraceVo<DtsAccountTrace> accountTraceVo = new AccountTraceVo<>();
    //获取分页用户角色数量
    Long count = dtsAccountTraceService.getAccountTraceCount();
    List<DtsAccountTrace> dtsAccountTraces = dtsAccountTraceService
        .getAccountTraceList(page, limit, sort, order, statusArray, username, mobile);
    accountTraceVo.setTotal(count);
    accountTraceVo.setTraceList(dtsAccountTraces);
    return ResponseUtil.ok(accountTraceVo);
  }

  //佣金审批
  @RequiresPermissionsDesc(menu = {"用户管理", "佣金管理"}, button = "审批")
  @RequiresPermissions(value = {"admin:account:approve"})
  @PostMapping ("/approve")
  public Object approveAgency(@RequestBody AccountTraceDTO accountTraceDTO) {
    //2 拒绝 0 申请 1 通过
    Boolean b = dtsAccountTraceService.approveAgency(accountTraceDTO);
    if (b) return ResponseUtil.ok();
    return ResponseUtil.fail();
  }

}
